package com.ncs.nc.config;

import com.ncs.nc.entity.Constant;
import com.ncs.nc.entity.RedisSubscriber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    /**
     * Redis链接工厂
     */
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * 缓存操作组件RedisTemplate的自定义配置
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(){
        //定义RedisTemplate实例
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        //设置Redis的链接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //指定key序列化策略为String序列化，Value为JDK自带的序列化策略
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        //指定hashKey序列化策略为String序列化，针对Hash散列存储
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        return redisTemplate;
    }

    /**
     * 缓存操作组件StringRedisTemplate
     * @return
     */
    @Bean
    public StringRedisTemplate stringRedisTemplate(){
        //采用默认配置即可，后续有自定义配置时就在这里添加
        StringRedisTemplate stringRedisTemplate=new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return stringRedisTemplate;
    }

//    @Autowired
//    private RedisSubscriber subscriber;
//    /**
//     *消息监听器适配
//     * @return
//     */
//    @Bean
//    public MessageListenerAdapter adapter(){
//        //将真正的监听处理逻辑委托给订阅者RedisSubscriber的onMessage方法进行监听处理
//        MessageListenerAdapter adapter=new MessageListenerAdapter(subscriber,"onMessage");
//        adapter.setSerializer(new GenericJackson2JsonRedisSerializer());
//        adapter.afterPropertiesSet();
//        return adapter;
//    }
//
//    /**
//     * 监听器容器--将订阅器绑定到容器
//     * @param adapter
//     * @return
//     */
//    @Bean
//    public RedisMessageListenerContainer container(MessageListenerAdapter adapter){
//        RedisMessageListenerContainer container=new RedisMessageListenerContainer();
//        //设置连接工厂
//        container.setConnectionFactory(redisConnectionFactory);
//        //设置监听器以及订阅的主题/频道
//        container.addMessageListener(adapter,new PatternTopic(Constant.REDIS_MSG_TOPIC));
//        return container;
//    }

}
